let userInfo = {
    username:'',
    password:''
    // moleColor: 1 // 后期注册放开摩尔颜色选择
} // 用户信息
let userOptions = JSON.parse(window.localStorage.getItem('userOptions'))
if (!userOptions)
{
    userOptions = {
        isRememberPw:false,
        isRememberUn:false
    }
}

let userData = {
    userID:'',
    serverID:''
    // 用户数据 存储用户的出生，摩尔豆，皮肤等关键信息，需要加密，不能明文存储修改
}

const loginButton = document.querySelectorAll('.login-button')
for (let i = 0; i < loginButton.length; i++)
{
    loginButton[i].addEventListener('mouseenter', function(e) {
        loginButton[i].children[0].src = './assets/ui/login/1127.png'
        loginButton[i].style.transform = 'scale(1.1)'
        loginButton[i].children[1].style.color = '#faff4b'
        if (!audio.Button.playing()) audio.Button.play()
        console.log(loginButton[i].children[1]);
    })
    loginButton[i].addEventListener('mouseleave', function() {
        loginButton[i].children[0].src = './assets/ui/login/1125.png'
        loginButton[i].style.transform = 'scale(1)'
        loginButton[i].children[1].style.color = '#AC3400'
        audio.Button.stop()
    })
    loginButton[i].addEventListener('mousedown', function() {
        loginButton[i].children[0].src = './assets/ui/login/1128.png'
        loginButton[i].style.transform = 'scale(1)'
        loginButton[i].children[1].style.color = '#faff4b'
    })
    loginButton[i].addEventListener('mouseup', function() {
        loginButton[i].children[0].src = './assets/ui/login/1127.png'
        loginButton[i].style.transform = 'scale(1.1)'
        loginButton[i].children[1].style.color = '#AC3400'
    })
}

const loginCheckBox = document.querySelectorAll('.login-checkbox')
for (let i = 0; i < loginCheckBox.length; i++)
{
    if (i === 0)
    {
        if (userOptions.isRememberUn) {
            loginCheckBox[i].dataset.check = 1
            loginCheckBox[i].children[0].src = './assets/ui/login/check.png'
        } else {
            loginCheckBox[i].dataset.check = 0
            loginCheckBox[i].children[0].src = './assets/ui/login/uncheck.png'
        }
    }
    else
    {
        if (userOptions.isRememberPw) {
            loginCheckBox[i].dataset.check = 1
            loginCheckBox[i].children[0].src = './assets/ui/login/check.png'
        } else {
            loginCheckBox[i].dataset.check = 0
            loginCheckBox[i].children[0].src = './assets/ui/login/uncheck.png'
        }
    }
    loginCheckBox[i].addEventListener('click', function(e) {
        if (loginCheckBox[i].dataset.check == 1)
        {
            loginCheckBox[i].dataset.check = 0
            loginCheckBox[i].children[0].src = './assets/ui/login/uncheck.png'
            i === 0 ?userOptions.isRememberUn = false : userOptions.isRememberPw = false
            window.localStorage.setItem('userOptions', JSON.stringify(userOptions))
        }
        else
        {
            loginCheckBox[i].dataset.check = 1
            loginCheckBox[i].children[0].src = './assets/ui/login/check.png'
            i === 0 ?userOptions.isRememberUn = true : userOptions.isRememberPw = true
            window.localStorage.setItem('userOptions', JSON.stringify(userOptions))
        }
        if (i === 1 && loginCheckBox[i].dataset.check == 1)
        {
            // 记住密码，需要弹框提示
            createDialog('仅在家中在线时选择记住密码！', {
                showYesButton:true
            }).then((result) => {
                if (result === 'yes') hideDialog()
            })
        }
    })
}

const loginInput = document.querySelectorAll('.login-input')
loginInput[0].value = window.localStorage.getItem('username') || ''
loginInput[1].value = window.localStorage.getItem('password') || ''
function login ()
{
    userInfo.username = loginInput[0].value
    userInfo.password = loginInput[1].value
    if (userInfo.username === '' || userInfo.password === '') return
    console.log(userInfo);
    let isfind = testAccount.find((item) => {
        return item.username === userInfo.username && item.password === userInfo.password
    })
    if (isfind) 
    {
        userData.userID = 'MOLE' + userInfo.username
        saveUserData()
        if (loginCheckBox[0].dataset.check == 1)
        {
            window.localStorage.setItem('username', userInfo.username)
        }
        else
        {
            window.localStorage.removeItem('username')
        }
        if (loginCheckBox[1].dataset.check == 1)
        {
            window.localStorage.setItem('password', userInfo.password)
        }
        else
        {
            window.localStorage.removeItem('password')
        }
        createServerSelectLayout()
        isLogin = true
    }
    else
    {
        createDialog('输入的账号或密码不正确，请重试！', {
            showYesButton:true,
            imgSrc: '../assets/dialog/problem.png'
        }).then((result) => {
            if (result === 'yes') hideDialog()
        })
    }
}

function saveUserData ()
{
    window.localStorage.setItem('userData', JSON.stringify(userData))
}

function getUserData ()
{
    return JSON.parse(window.localStorage.getItem('userData')) || null
}

function removeUserData ()
{
    window.localStorage.removeItem('userData')
}

function register ()
{
    // 暂未开放
    createDialog('暂未开放注册！', {
        showYesButton:true,
        imgSrc: '../assets/dialog/problem.png'
    }).then((result) => {
        if (result === 'yes') hideDialog()
    })
}

// 创建登录页面
setTimeout(() => {
    createLoginLayout()
}, 1000);

